<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Category_Products extends CI_Controller {

	public function index()
	{
	    $this->load->model('Product_groups_model');
	    $id = null;
	    $description = null;
	    if ($this->input->post('id')) {
	        $id = $this->input->post('id');
	    }
	    if ($this->input->post('description')) {
	        $description = $this->input->post('description');
	    }
	    $groups = $this->Product_groups_model->getAll();
	    if ($id != null || $description != null) {
	        $groups = $this->Product_groups_model->search($id, $description);
	    }
	    $vars['groups'] = $groups;
	    $vars['id'] = $id;
	    $vars['description'] = $description;
	    $vars['content_view'] = 'groups_list_index';
	    $vars['menu'] = 'products';
	    $this->load->view('template', $vars);
	}

	public function add()
	{
	    $this->load->model('Product_groups_model');
            $vars['productGroups'] = $this->Product_groups_model->getAll();
	    $vars['content_view'] = 'product_groups_form';
	    $vars['menu'] = 'products';
	    $this->load->view('template', $vars);
	}

	public function evalue()
	{
	    if ($this->input->post('description')) {
	        $desc = $this->input->post('description');
	        $this->load->model('Product_groups_model');
	        $dataGroup = $this->Product_groups_model->find($desc);
	        if ($dataGroup) {
	            echo "true";
	        } else {
	            echo "false";
	        }
	    }
	}

	public function submit()
	{
	    $id = $this->input->post('group_id');
	    // $this->load->view('prueba', $id);
	    if ($this->_validate_submit() === FALSE) {
	        if (isset($id) && is_numeric($id)) {
	            $this->edit($id);
	        } else {
	            $this->add();
	        }
	        return;
	    } else {
	        $this->load->model('Product_groups_model');
	        $data = array();
	        $data['description'] = $this->input->post('description');	    
	    }
	    if (isset($id) && is_numeric($id)) {
	        $this->Product_groups_model->update($id, $data);
	    } else {
	        $this->Product_groups_model->add($data);
	    }
	    redirect('/category_products/', 'refresh');
	}

	public function edit($code)
	{
		$this->load->model('Product_groups_model');
	    $vars['group'] = $this->Product_groups_model->findCode($code);	    
	    $vars['content_view'] = 'product_groups_form';
	    $vars['menu'] = 'products';
	    $this->load->view('template', $vars);
	}

	public function delete($code)
	{
	    $this->load->model('Product_groups_model');
        $this->Product_groups_model->delete($code);
        $this->load->model('Products_model');
        $this->Products_model->deleteGroup($code);
        $this->index();        
	}

	private function _validate_submit()
	{
	    $this->form_validation->set_rules('description', 'Descripcion','trim|required');
	    return $this->form_validation->run();
	}
}